/*
 * @Author: yangrongxin
 * @Date: 2021-08-25 16:16:53
 * @LastEditors: guoju
 * @LastEditTime: 2022-01-07 11:25:36
 */

import Taro from '@tarojs/taro';
import { pay, getPayTicket } from '@/services/global';
import { getPayParams, checkPayStatus, getParamsWithOrderId } from './service';

/* 调用支付方法进行跳转 -- 免费 */
export const payFn = (resOrderId, name, measureTableId, isFree = false, callback = () => {}) => {
  if (isFree) {
    pay({
      orderId: resOrderId,
    }).then((payRes: any = {}) => {
      if (!payRes) return;
      // 支付完成之后当前进入到开始答题模式
      const { measureTableRecordList = [] } = payRes;
      const recordItem = measureTableRecordList.find((fi: any) => fi.measureTableId === measureTableId);
      const { measureTableRecordId, evaluationId, evaluationType } = recordItem;
      callback && callback();
      Taro.navigateTo({
        url: `/EvaluationModule/pages/CheckboxAnwser/index?measureTableId=${measureTableId}&title=${name}&measureTableRecordId=${measureTableRecordId}&evaluationId=${evaluationId}&evaluationType=${evaluationType}`,
      });
      console.log('payRes', payRes);
    });
  } else {
    getPayTicket({
      orderId: resOrderId,
    }).then((ticketRes: any) => {
      if (!ticketRes) return;
      getPayParams(ticketRes).then((paramsRes: any) => {
        callback && callback();
        Taro.requestPayment({
          ...paramsRes,
          paySign: paramsRes.sign,
        })
          .then((ress: any) => {
            if (!ress) return;
            // 校验当前的支付状态 支付完成之后跳转评测
            checkPayStatusFn(ticketRes, resOrderId, name, measureTableId);
          })
          .catch((error: any) => {
            console.log('error', error);
          });
      });
    });
  }
};

/* 校验当前支付状态的方法 */
export const checkPayStatusFn = (ticketRes, ResOrderId, name, measureTableId) => {
  // ticketRes 当前的票据信息
  let timer: any = null;
  checkPayStatus(ticketRes).then((statusStr: any) => {
    switch (statusStr) {
      // 正在支付中
      case 'toPay':
        Taro.showLoading({
          title: '确认中...',
        }).then(() => {
          timer = setTimeout(() => {
            checkPayStatusFn(ticketRes, ResOrderId, name, measureTableId);
            clearTimeout(timer);
          }, 2000);
        });
        break;
      // 当前已经支付完成
      case 'paid':
        // Taro.showToast({
        //   title: '支付成功，即将开始测评，请稍候！',
        //   icon: 'none',
        // }).then(() => {
        // 根据当前的支付情况判断是不是跳转到支付完成界面 还是直接进行答题
        Taro.hideLoading();
        // 通过orderId调用接口获取测评需要的数据
        getParamsWithOrderId({
          orderId: ResOrderId,
        }).then((paramsRes: any) => {
          const { measureTableRecordId, evaluationId, evaluationType } = paramsRes;
          Taro.navigateTo({
            url: `/EvaluationModule/pages/CheckboxAnwser/index?measureTableId=${measureTableId}&title=${name}&measureTableRecordId=${measureTableRecordId}&evaluationId=${evaluationId}&evaluationType=${evaluationType}`,
          });
        });
        console.log('支付完成，等待进行跳转');
        // });
        break;
      // 当前是支付失败 -- 支付超时
      case 'callFail':
        Taro.showToast({
          title: '支付超时',
          icon: 'none',
          duration: 1500,
        }).then(() => {
          Taro.hideLoading();
          payFn(ResOrderId, name, measureTableId);
        });
        break;
    }
  });
};
